package ylsb.service;

import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.*;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;
import ylsb.dao.YiyongjieguoMapper;
import ylsb.model.*;
import ylsb.support.StringHandle;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

@Service
public class YiyongjieguoService {

    @Resource
    YiyongjieguoMapper ddMapper;

    @Resource
    YiyonghaocaiService ddYiyonghaocaiService;

    @Resource
    YltpuserService ddYltpuserService;

    @Resource
    YltpXiangmuService ddYltpXiangmuService;


    /* public Integer checkCountByPinzhongandUserId(int ppPinzhong,String ppuserId){
        YiyongjieguoExample mmExample=new YiyongjieguoExample();
        YiyongjieguoExample.Criteria criteria = mmExample.createCriteria();
        criteria.andPinzhongEqualTo(ppPinzhong).andUseridEqualTo(ppuserId).andResultEqualTo("1");
        Object mmCount=ddMapper.selectByExample(mmExample).size();
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }*/
   public List<Map<String, Object>> findPinpaiJieguo(String ppxiangmuId,String ppuserId,String mmPinzhonxuhao,String mmPinpaixuhao){
       return ddMapper.findPinpaiJieguo(ppxiangmuId,ppuserId,mmPinzhonxuhao,mmPinpaixuhao);
   }

    public Yiyongjieguo checkexistByUserId(int ppPinzhong, String ppPinpai, String ppuserId,String ppxiangmuId ){
        YiyongjieguoExample mmExample=new YiyongjieguoExample();
        YiyongjieguoExample.Criteria criteria = mmExample.createCriteria();
        criteria.andPinzhongEqualTo(ppPinzhong).andPinpaiEqualTo(ppPinpai).andUseridEqualTo(ppuserId).andXinghaoEqualTo(ppxiangmuId);
        return ddMapper.selectByExample(mmExample).size()>0? ddMapper.selectByExample(mmExample).get(0): null;
    }
    public Integer checkCountByPinzhongandUserId(int ppPinzhong,String ppuserId,String ppxiangmuId){
        YiyongjieguoExample mmExample=new YiyongjieguoExample();
        YiyongjieguoExample.Criteria criteria = mmExample.createCriteria();
        criteria.andPinzhongEqualTo(ppPinzhong).andUseridEqualTo(ppuserId).andXinghaoEqualTo(ppxiangmuId).andResultEqualTo("1");
        Object mmCount=ddMapper.selectByExample(mmExample).size();
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }

    public boolean modify(Yiyongjieguo yiyongjieguo){return ddMapper.updateByPrimaryKeySelective(yiyongjieguo)==1;}

    public boolean add(Yiyongjieguo yiyongjieguo){
        return ddMapper.insert(yiyongjieguo)==1;
    }

    public Integer getResultCount(String mmxiangmuId,String mmuserId,String mmresult){
        Object mmCount = ddMapper.getResultCount(mmxiangmuId,mmuserId,mmresult);
        return mmCount == null ? 0 : Integer.parseInt(mmCount.toString());
    }

    public void daochuqianzibiao(@RequestParam("xiangmuid") String ppxiangmuid,
                                 @RequestParam("userid") String ppuserid,
                                 HttpServletRequest request, HttpServletResponse response){

        response.reset();
        response.setContentType("application/x-download;charset=utf-8");
        List<Map<String,Object>> mmList = ddYiyonghaocaiService.findDaochuPinzhongAll(ppxiangmuid);
        YltpUser mmuser=ddYltpuserService.findOne(ppuserid);
        String mmusername=mmuser.getUsername();
        YltpXiangmu mmxiangmu=ddYltpXiangmuService.findOne(ppxiangmuid);
        String mmxiangmuname=mmxiangmu.getXiangmuname();
        OutputStream os=null;
        WritableWorkbook wbook=null;
        try
        {
            os = new BufferedOutputStream(response.getOutputStream());//得到输出流的语句
            //response.getOutputStream();// 取得输出流
            response.reset();// 清空输出流
            SimpleDateFormat mmDateFormat = new SimpleDateFormat("yyyyMMdd-HHmmss");
            String wenjiantitle = mmxiangmuname+mmusername+"专家签字表"; //文件名
            String tmptitle = "医用耗材（试剂）遴选专家预选意见-"+mmxiangmuname; // 标题
            response.setHeader("Content-disposition", "attachment; filename="+new String(wenjiantitle.getBytes(),"iso8859-1")+".xls");// 设定输出文件头
            response.setContentType("application/vnd.ms-excel");// 定义输出类型
            wbook = Workbook.createWorkbook(os); // 建立excel文件
            WritableSheet wsheet = wbook.createSheet(tmptitle, 0); // sheet名称
            // 设置excel标题
            WritableFont wfont = new WritableFont(WritableFont.ARIAL, 14,WritableFont.NO_BOLD,false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); //设置表格样式

            WritableCellFormat wcfFC = new WritableCellFormat(wfont);
            wcfFC.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN); // 添加边框
            wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            wcfFC.setAlignment(Alignment.CENTRE);

            WritableCellFormat wcfFC3 = new WritableCellFormat(wfont);
            wcfFC3.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            wcfFC3.setAlignment(Alignment.CENTRE);
            //wcfFC.setBackground(Colour.AQUA);
            Label label=new Label(0, 0, tmptitle, wcfFC3);

            WritableCellFormat cellFormat=new WritableCellFormat();
            cellFormat.setAlignment(jxl.format.Alignment.CENTRE);
            cellFormat.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
            cellFormat.setWrap(true);
            //cellFormat.setBackground(Colour.AQUA);
            cellFormat.setFont(wfont);
            label.setCellFormat(cellFormat);
            wsheet.addCell(label);
            wsheet.addCell(new Label(0, 0, tmptitle, wcfFC3));
            /*wsheet.addCell(new Label(7, 0, tmptitle, wcfFC));*/
            wsheet.setRowView(0,800); //第一行高度
            wsheet.setRowView(1,800); //第一行高度
            wsheet.setRowView(2,660); //第一行高度
            wsheet.mergeCells(0, 0, 18, 0);  //合并单元格(第一列的第一行和第十三列的第一行合并)
            wsheet.setColumnView(0,5); //序号宽度
            wsheet.setColumnView(1,6); //品种名宽度
            wsheet.setColumnView(2,5); //品种序号宽度
            wsheet.setColumnView(3,5); //品牌序号宽度
            wsheet.setColumnView(4,5); //型号序号宽度
            wsheet.setColumnView(5,8); //宽度
            wsheet.setColumnView(6,8); //宽度
            wsheet.setColumnView(7,8); //宽度
            wsheet.setColumnView(8,8); //宽度
            wsheet.setColumnView(9,8); //宽度
            wsheet.setColumnView(10,8); //宽度
            wsheet.setColumnView(11,8); //宽度
            wsheet.setColumnView(12,8); //宽度
            wsheet.setColumnView(13,8); //宽度
            wsheet.setColumnView(14,8); //宽度
            wsheet.setColumnView(15,8); //宽度
            wsheet.setColumnView(16,8); //宽度
            wsheet.setColumnView(17,8); //宽度
            wsheet.setColumnView(18,8); //宽度

            // 开始生成主体内容
            wfont = new jxl.write.WritableFont(WritableFont.createFont("宋体"), 8,WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
            wcfFC = new WritableCellFormat(wfont);
            wcfFC.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
            wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            wcfFC.setAlignment(Alignment.CENTRE);
            wcfFC.setWrap(true);
            // 不需要边框和居中的样式
            WritableFont wfont2 = new WritableFont(WritableFont.ARIAL, 10,WritableFont.NO_BOLD,false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK); //设置表格样式
            WritableCellFormat wcfFC2 = new WritableCellFormat(wfont2);
            wcfFC2.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 垂直居中
            wcfFC2.setAlignment(Alignment.LEFT);
            wcfFC2.setWrap(true);
            //  在  1 列  2 行 写入内容
            wsheet.addCell(new Label(0, 1, "投票人(签字):投票内容已核对无误。"+mmusername,wcfFC2));
            // 把  0 列 2 行 到 9 列 2 行合并
            wsheet.mergeCells(0, 1, 10, 1);

            wsheet.addCell(new Label(11, 1, "投票日期:"+StringHandle.shorttiemString(),wcfFC2));
            wsheet.mergeCells(11, 1, 18, 1);

            // 第三行 内容
            wsheet.addCell(new Label(0, 2, "序号",wcfFC));
            wsheet.addCell(new Label(1, 2, "品种名",wcfFC));
            wsheet.addCell(new Label(2, 2, "品种序号",wcfFC));
            wsheet.addCell(new Label(3, 2, "品牌",wcfFC));
            wsheet.addCell(new Label(4, 2, "型号  ",wcfFC));
            wsheet.addCell(new Label(5, 2, "平台编码",wcfFC));
            wsheet.addCell(new Label(6, 2, "产品名称",wcfFC));
            wsheet.addCell(new Label(7, 2, "产品规格",wcfFC));
            wsheet.addCell(new Label(8, 2, "产品型号",wcfFC));
            wsheet.addCell(new Label(9, 2, "注册证号",wcfFC));
            wsheet.addCell(new Label(10, 2, "生产商（品牌）",wcfFC));
            wsheet.addCell(new Label(11, 2, "计价单位",wcfFC));
            wsheet.addCell(new Label(12, 2, "平台情况",wcfFC));
            wsheet.addCell(new Label(13, 2, "进口/国产",wcfFC));
            wsheet.addCell(new Label(14, 2, "平台单价（元）",wcfFC));
            wsheet.addCell(new Label(15, 2, "是否医保",wcfFC));
            wsheet.addCell(new Label(16, 2, "备注",wcfFC));
            wsheet.addCell(new Label(17, 2, "适用原则",wcfFC));
            wsheet.addCell(new Label(18, 2, "预选意见",wcfFC));


            // 表头结束

            wfont = new jxl.write.WritableFont(WritableFont.createFont("宋体"), 8,WritableFont.NO_BOLD,false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
            wcfFC = new WritableCellFormat(wfont);
            wcfFC.setBorder(jxl.format.Border.ALL,jxl.format.BorderLineStyle.THIN);
            wcfFC.setVerticalAlignment(VerticalAlignment.CENTRE);   //设置居中对齐
            wcfFC.setAlignment(Alignment.CENTRE);
            wcfFC.setWrap(true);

            int count=mmList.size();
            int mmpinzhongKaishihang=3;
            int mmpinpaiKaishihang=3;
            int mmxinghaoKaishihang=3;


            for(int i=0;i<count;i++){
                Map<String,Object> mmPinzhongMap=mmList.get(i);

                String mmPinzhongOne = mmPinzhongMap.get("pinzhongxuhao") == null ? "" : mmPinzhongMap.get("pinzhongxuhao").toString();
                String mmPinzhongming = mmPinzhongMap.get("pinzhongming") == null ? "" : mmPinzhongMap.get("pinzhongming").toString();
                int mmHebingshu = 0;

                List<Map<String,Object>> mmPinpailist = ddYiyonghaocaiService.findPinpaiByPinzhong(ppxiangmuid,mmPinzhongOne);
                if (mmPinpailist.size()>0){
                    mmHebingshu = mmPinpailist.size()-1;
                }
                List<Map<String,Object>> mmPinpaiFenlei = ddYiyonghaocaiService.findPinpaiFenlei(ppxiangmuid,mmPinzhongOne);
                for (int j = 0; j < mmPinpaiFenlei.size(); j++) {
                    int s=0;
                    Map<String,Object> mmPinpaiMap = mmPinpaiFenlei.get(j);
                    int mmPinpaiHebingshu = 0;
                    if (mmPinpaiMap!=null){
                        String mmPinpaiOne = mmPinpaiMap.get("pinpaixuhao") == null ? "" : mmPinpaiMap.get("pinpaixuhao").toString();
                        List<Map<String,Object>> mmXinhaoList = ddYiyonghaocaiService.findXinghaoJieguoByPinpai(mmPinzhongOne,mmPinpaiOne,ppxiangmuid,ppuserid);
                        if (mmXinhaoList.size()>0){
                            mmPinpaiHebingshu = mmXinhaoList.size()-1;
                        }
                        for (int k = 0; k < mmXinhaoList.size(); k++) {
                            Map<String, Object> mmXinhaoMap = mmXinhaoList.get(k);
                            if (mmXinhaoMap != null) {
                                String mmXuhao = mmXinhaoMap.get("ordernumber")==null ? "" :mmXinhaoMap.get("ordernumber").toString();
                                String mmxinghaochaifen=mmXinhaoMap.get("xinghaoxuhao")==null ? "" :mmXinhaoMap.get("xinghaoxuhao").toString();
                                String mmpingtaibianma=mmXinhaoMap.get("pingtaibianma")==null ? "" :mmXinhaoMap.get("pingtaibianma").toString();
                                String mmchanpinmingcheng=mmXinhaoMap.get("chanpinmingcheng")==null ? "" :mmXinhaoMap.get("chanpinmingcheng").toString();
                                String mmguige=mmXinhaoMap.get("chanpinguige")==null ? "" :mmXinhaoMap.get("chanpinguige").toString();
                                String mmxinghao=mmXinhaoMap.get("chanpinxinghao")==null ? "" :mmXinhaoMap.get("chanpinxinghao").toString();
                                String mmzhucezhenghao=mmXinhaoMap.get("zhucezhenghao")==null ? "" :mmXinhaoMap.get("zhucezhenghao").toString();
                                String mmshengchanshang=mmXinhaoMap.get("shengchanshangpinpai")==null ? "" :mmXinhaoMap.get("shengchanshangpinpai").toString();
                                String mmjijiadanwei=mmXinhaoMap.get("jijiadanwei")==null ? "" :mmXinhaoMap.get("jijiadanwei").toString();
                                String mmpingtaiqingkuang=mmXinhaoMap.get("pingtaiqingkuang")==null ? "" :mmXinhaoMap.get("pingtaiqingkuang").toString();
                                String mmjinkouguochan=mmXinhaoMap.get("jinkouguochan")==null ? "" :mmXinhaoMap.get("jinkouguochan").toString();
                                String mmpingtaidanjia=mmXinhaoMap.get("pingtaidanjia")==null ? "" :mmXinhaoMap.get("pingtaidanjia").toString();
                                String mmgongnengyongtu=mmXinhaoMap.get("gongnengyongtu")==null ? "" :mmXinhaoMap.get("gongnengyongtu").toString();
                                String mmresult=mmXinhaoMap.get("result")==null ? "" :mmXinhaoMap.get("result").toString();
                                String mmshifouyibao=mmXinhaoMap.get("text10")==null ? "" :mmXinhaoMap.get("text10").toString();
                                String mmbeizhu=mmXinhaoMap.get("text11")==null ? "" :mmXinhaoMap.get("text11").toString();
                                String mmbiaojueren=mmXinhaoMap.get("username")==null ? "" :mmXinhaoMap.get("username").toString();
                                String mmshiyongyuanze=mmXinhaoMap.get("shiyongyuanzeid")==null ? "" :mmXinhaoMap.get("shiyongyuanzeid").toString();
                                String mmyijian="";
                                if("1".equals(mmresult)){
                                    mmyijian="同意";
                                }else if("0".equals(mmresult)){
                                    mmyijian="不同意";
                                }else {
                                    mmyijian="不同意";
                                }
                                wsheet.setRowView(mmxinghaoKaishihang,660); //第三行高度
                                wsheet.addCell(new Label(0, mmxinghaoKaishihang,mmXuhao,wcfFC));
                                wsheet.addCell(new Label(4, mmxinghaoKaishihang, mmxinghaochaifen,wcfFC));
                                wsheet.addCell(new Label(5, mmxinghaoKaishihang,mmpingtaibianma,wcfFC));
                                wsheet.addCell(new Label(6, mmxinghaoKaishihang,mmchanpinmingcheng,wcfFC));
                                wsheet.addCell(new Label(7, mmxinghaoKaishihang,mmguige,wcfFC));
                                wsheet.addCell(new Label(8, mmxinghaoKaishihang,mmxinghao,wcfFC));
                                wsheet.addCell(new Label(9, mmxinghaoKaishihang,mmzhucezhenghao,wcfFC));
                                wsheet.addCell(new Label(10, mmxinghaoKaishihang,mmshengchanshang,wcfFC));
                                wsheet.addCell(new Label(11, mmxinghaoKaishihang,mmjijiadanwei,wcfFC));
                                wsheet.addCell(new Label(12, mmxinghaoKaishihang,mmpingtaiqingkuang,wcfFC));
                                wsheet.addCell(new Label(13, mmxinghaoKaishihang,mmjinkouguochan,wcfFC));
                                wsheet.addCell(new Label(14, mmxinghaoKaishihang,mmpingtaidanjia,wcfFC));
                                wsheet.addCell(new Label(15, mmxinghaoKaishihang,mmshifouyibao,wcfFC));
                                wsheet.addCell(new Label(16, mmxinghaoKaishihang,mmbeizhu,wcfFC));
                                wsheet.addCell(new Label(17, mmxinghaoKaishihang,mmshiyongyuanze,wcfFC));
                                wsheet.addCell(new Label(18, mmxinghaoKaishihang,mmyijian,wcfFC));
                                mmxinghaoKaishihang = mmxinghaoKaishihang+1;
                            }
                        }
                        wsheet.addCell(new Label(3, mmpinpaiKaishihang, mmPinpaiOne,wcfFC));
                        wsheet.mergeCells(3, mmpinpaiKaishihang, 3, mmpinpaiKaishihang+mmPinpaiHebingshu);
                        wsheet.mergeCells(17, mmpinpaiKaishihang, 17, mmpinpaiKaishihang+mmPinpaiHebingshu);
                        wsheet.mergeCells(18, mmpinpaiKaishihang, 18, mmpinpaiKaishihang+mmPinpaiHebingshu);
                        mmpinpaiKaishihang = mmpinpaiKaishihang+mmPinpaiHebingshu+1;
                    }
                }
                wsheet.addCell(new Label(2, mmpinzhongKaishihang, mmPinzhongOne,wcfFC));
                wsheet.mergeCells(2, mmpinzhongKaishihang, 2, mmpinzhongKaishihang+mmHebingshu);
                wsheet.addCell(new Label(1, mmpinzhongKaishihang, mmPinzhongming,wcfFC));
                wsheet.mergeCells(1, mmpinzhongKaishihang, 1, mmpinzhongKaishihang+mmHebingshu);
                mmpinzhongKaishihang = mmpinzhongKaishihang+mmHebingshu+1;

            }
            wbook.write(); // 写入文件
            wbook.close();
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }finally{
            if(os!=null){
                try {
                    os.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }

    public List<Map<String,Object>> findResultBypinzhong(String mmXiangmuId,String mmUserId,String mmpinzhong){
        return ddMapper.findResultBypinzhong(mmXiangmuId,mmUserId,mmpinzhong);
    }

    public List<Map<String,Object>> findPinpaiResultAll(String mmXiangmuId,String mmpinzhong){
        return ddMapper.findPinpaiResultAll(mmXiangmuId,mmpinzhong);
    }
    public Yiyongjieguo findOne(String id){
        return ddMapper.selectByPrimaryKey(id);
    }

    public List<Map<String,Object>> findByxiangmuId(String mmXiangmuId){
        return ddMapper.findByxiangmuId(mmXiangmuId);
    }

    public List<Map<String,Object>> getPinzhongpaixu(String mmXiangmuId){
        return ddMapper.getPinzhongpaixu(mmXiangmuId);
    }

    public List<Map<String, Object>> getPinpaipaixuByPinzhong(String ppxiangmuId,String mmPinzhongOne){
        return ddMapper.getPinpaipaixuByPinzhong(ppxiangmuId,mmPinzhongOne);
    }
    public List<Map<String,Object>> getAgreeListByXiangMuIdAndPinZhong(String mmXiangmuId,int mmPinzhong){
        return  ddMapper.getAgreeListByXiangMuIdAndPinZhong(mmXiangmuId,mmPinzhong);
    }

}
